<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="small"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['putStorage:proMatStart:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['putStorage:proMatStart:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['putStorage:proMatStart:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="small"
          :disabled="single"
          @click="check_submit = true"
          v-hasPermi="['putStorage:proMatStart:submit']"
        >
          提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport(1)"
          v-hasPermi="['putStorage:proMatStart:export']"
        >
          EXCEL
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport(2)"
          v-hasPermi="['putStorage:proMatStart:export']"
        >
          子项EXCEL
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#bb0331">通过</Tag>
      <Tag type="dot" size="default" color="#0323af">检验</Tag>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :height="tableH"
      @row-click="clickRtm"
      :data="proMatStartList"
      :cell-style="iCellStyle2"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" min-width="5%" align="center" />
      <el-table-column
        label="退料单号"
        :show-overflow-tooltip="true"
        min-width="12%"
        align="center"
        prop="rtmDjbh"
      />
      <el-table-column
        label="退料日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtmTlrq"
        min-width="10%"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtmTlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="退料部门"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="rtmBmdm"
      />
      <el-table-column
        label="退料仓库"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="rtmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.rtmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="制造车间"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="rtmCj"
      >
        <template #default="scope">
          <dict-tag :options="itCarOptions" :value="scope.row.rtmCj" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="rtmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="rtmNgdesc"
      />
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="rtmJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtmJlrq"
        min-width="10%"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="rtmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtmShrq"
        min-width="10%"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="rtmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rtmGzrq"
        min-width="10%"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="150px"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAddChild(scope.row)"
            :disabled="scope.row.rtmZtbz != 1 || scope.row.rtmTjflag != 0"
            v-hasPermi="['putStorage:proMatStart:edit']"
          >
            添加物料
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-s-promotion"
            @click="printHandle(scope.row)"
            v-hasPermi="['putStorage:proMatStart:submit']"
          >
            单据打印
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 子项Table   -->
    <el-table
      border
      stripe
      highlight-current-row
      :height="tableHChild"
      :data="rtdDetList"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
    >
      <el-table-column label="#" type="index" align="center" />
      <el-table-column
        label="客户代码"
        width="160"
        align="center"
        prop="itmCustwldm"
      />
      <el-table-column
        label="物料代码"
        width="120"
        align="center"
        prop="rtdWldm"
      />
      <el-table-column
        label="规格描述"
        min-width="150"
        align="center"
        prop="itmPmgg"
      />
      <el-table-column label="单位" width="80" align="center" prop="itmDw">
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="储位信息" :show-overflow-tooltip="true" align="center" prop="rtdCwdm">
        <template #default="scope">
          <dict-tag :options="cWOptions" :value="scope.row.rtdCwdm" />
        </template>
      </el-table-column> -->
      <el-table-column
        label="最小包装"
        width="90"
        align="center"
        prop="itmMinbz"
      />
      <el-table-column
        label="包装数量"
        width="90"
        align="center"
        prop="rtdBzsl"
      />
      <el-table-column
        label="申请数量"
        width="90"
        align="center"
        prop="rtdSqsl"
      />
      <el-table-column
        label="入库数量"
        width="90"
        align="center"
        prop="rtdRksl"
      />
      <!-- <el-table-column label="入库人员" align="center" prop="rtdRkry" /> -->
      <el-table-column
        label="入库日期"
        width="100"
        align="center"
        prop="rtdRkrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rtdRkrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            :disabled="single"
            v-hasPermi="['putStorage:proMatStart:remove']"
            @click="deleteChild(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-col :span="24">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="daterangeRtmJlrq"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              :picker-options="pickerOptions"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="退料单号" prop="rtmDjbh">
            <el-input
              v-model="queryParams.rtmDjbh"
              placeholder="请输入退料单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="入库仓库" prop="rtmCkdm">
            <el-select
              v-model="queryParams.rtmCkdm"
              style="width: 240px"
              clearable
              filterable
              placeholder="请选择仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="rtmZtbz">
            <RadioGroup v-model="queryParams.rtmZtbz">
              <Radio style="margin-left: 100px" label="1">未检验</Radio>
              <Radio style="margin-left: 20px" label="2">已检验</Radio>
              <Radio style="margin-left: 20px" label="">全部</Radio>
            </RadioGroup>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改生产退料入库对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="退料日期" prop="rtmTlrq">
            <el-date-picker
              clearable
              size="small"
              v-model="form.rtmTlrq"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择退料日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="退料仓库" prop="rtmCkdm">
            <el-select
              v-model="form.rtmCkdm"
              :disabled="title == '修改生产退料入库'"
              placeholder="请选择退料仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制造车间" prop="rtmCj">
            <el-select
              v-model="form.rtmCj"
              :disabled="title == '修改生产退料入库'"
              placeholder="请选择车间"
            >
              <el-option
                v-for="item in itCarOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="rtmDesc">
            <el-input
              type="textarea"
              v-model="form.rtmDesc"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加子项对话框 -->
    <el-dialog
      v-model="addChildOpen"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 子项搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-row>
          <el-col :span="8">
            <el-form-item label="物料代码" prop="rtdWldm">
              <el-input
                v-model="queryChildParams.rtdWldm"
                placeholder="请输入物料代码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格描述" prop="itmPmgg">
              <el-input
                v-model="queryChildParams.itmPmgg"
                placeholder="请输入规格描述"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item style="display: flex; justify-content: flex-end">
              <el-button type="primary" @click="getSerachChild">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--子项搜索数据框-->
      <el-form>
        <el-table
          highlight-current-row
          v-loading="loading"
          border
          stripe
          :data="searchChildList"
          :height="tableHAdd"
          @scrollBottom="load2"
          ref="rtdDetTable"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column type="index" width="55" align="center" />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="rtdWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            min-width="40"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="储位信息"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="rtdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="cWOptions" :value="scope.row.rtdCwdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="最小包装"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="itmMinbz"
          />
          <el-table-column
            label="已申请数"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="rtdYsql"
          />
          <el-table-column
            label="需申请数"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="rtdSqsl"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                v-model="scope.row.rtdSqsl"
              ></el-input>
            </template>
          </el-table-column>
        </el-table>
        <!-- 底部确定取消 -->
        <el-form-item
          style="margin-top: 25px; display: flex; justify-content: flex-end"
        >
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="searchAddRtd">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 提交 -->
    <Modal v-model="check_submit" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认</span>
      </p>
      <div style="text-align: center">
        <p>是否将【入库单号：{{ this.rtmDjbhs }}】提交审核？</p>
        <p>一旦提交数据无法更改，需审核人员撤回后方能更改。</p>
      </div>
      <div slot="footer">
        <Button type="success" size="large" long @click="handleSubMit"
          >提交</Button
        >
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  listProMat,
  getProMat,
  delProMat,
  addProMat,
  updateProMat,
  exportProMat,
  serachChild,
  listRtdDet,
  insRtdDet,
  removeRtdDet,
} from "@/api/putStorage/proMatStart";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { listckMxDet, submitCkMx } from "@/api/output/picking";
import { printOrder } from "@/utils/ruoyi";

export default {
  name: "ProMatStart",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: (document.body.clientHeight - 280) / 2,
      tableHAdd: document.body.clientHeight - 320,
      // 非单个禁用
      single: true,
      listData: null,
      //提交模态
      check_submit: false,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      //有效存储仓库
      storageOptions: [],
      //所有存储仓库
      allStorageOptions: [],
      //单位字典
      unitFlagOptions: [],
      // 车间字典
      itCarOptions: [],
      //默认储位字典
      cWOptions: [],
      // 总条数
      total: 0,
      total2: 0,
      // 生产退料入库表格数据
      proMatStartList: [],
      // 搜索到可进行退货的物料
      searchChildList: [],
      //生产退料入库详细表数据
      rtdDetList: [],
      //添加物料保存的单号
      rtmDjbhs: null,
      //选中的添加物料
      checkSerachList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示添加子项弹出层
      addChildOpen: false,
      // 建立日期时间范围
      daterangeRtmJlrq: [
        this.$moment().startOf("month").format(),
        this.$moment().endOf("month").format(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        rtmDjbh: null,
        rtmZtbz: "1",
        rtmJlrq: null,
        index: 1,
      },
      // 子项查询参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 40,
        rtdWldm: null,
        itmPmgg: null,
        rtdDjbh: null,
        rtdCkdm: null,
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 表单校验
      rules: {
        rtmCkdm: [
          { required: true, message: "入库仓库不能为空", trigger: "blur" },
        ],
        rtmCj: [{ required: true, message: "车间不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;
      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    getAll().then((response) => {
      this.allStorageOptions = response.data;
      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //存放位置
    getAll().then((response) => {
      this.cWOptions = response.data;
      for (let item of this.cWOptions) {
        item.dictLabel = item.storageDefaultstorage;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //制造车间字典
    this.getDicts("Manufacturing_plant").then((response) => {
      this.itCarOptions = response.data;
    });
  },
  methods: {
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.rtdRksl) {
        return "color:#bb0331";
      }
    },
    iCellStyle2: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.rtmZtbz == "1" && row.rtmTjflag == "1") {
        return "color:#0fac09";
      } else if (row.rtmTjflag == "1" && row.rtmZtbz == "6") {
        return "color:#bb0331";
      } else if (row.rtmTjflag == "1" && row.rtmZtbz == "2") {
        return "color:#0323af";
      }
    },
    /** 查询生产退料入库列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRtmJlrq && "" != this.daterangeRtmJlrq) {
        this.queryParams.params["beginRtmJlrq"] = this.moment(
          this.daterangeRtmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endRtmJlrq"] = this.moment(
          this.daterangeRtmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listProMat(this.queryParams).then((response) => {
        this.proMatStartList = [];
        this.rtdDetList = [];
        this.proMatStartList = this.proMatStartList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRtm(row);
        } else if (
          this.proMatStartList != null &&
          this.proMatStartList.length > 0
        ) {
          this.clickRtm(this.proMatStartList[0]);
        }
      });
    },
    /** 单据打印 */
    printHandle(row) {
      const rtmDjbh = row.rtmDjbh;
      const data = { rtmDjbh: rtmDjbh };
      if (row.rtmTjflag != "1") {
        this.$confirm("当前单据还未提交，是否提交并执行打印？", "温馨提醒", {
          confirmButtonText: "提交并打印",
          cancelButtonText: "取消",
          type: "info",
        }).then((res) => {
          this.handleSubMit().then((res) => {
            printOrder(
              "/putStorageData/proMat/exportIncomingGoodsTemplate",
              data,
              "生产退料单据"
            );
          });
        });
        return;
      }
      printOrder(
        "/putStorageData/proMat/exportIncomingGoodsTemplate",
        data,
        "生产退料单据"
      );
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 40;
        this.queryParams.pageNum++;
        listProMat(this.queryParams).then((response) => {
          this.proMatStartList = this.proMatStartList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.addChildOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rtmId: null,
        rtmDjbh: null,
        rtmTlrq: new Date(),
        rtmCkdm: null,
        rtmBmdm: null,
        rtmDesc: null,
        rtmTjflag: null,
        rtmNgdesc: null,
        rtmZtbz: null,
        rtmJlry: null,
        rtmJlrq: null,
        rtmShry: null,
        rtmShrq: null,
        rtmGzry: null,
        rtmGzrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.proMatStartList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRtmJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 单据单击事件*/
    clickRtm(row) {
      if (row != null) {
        this.ids = row.rtmId;
        this.rtmDjbhs = row.rtmDjbh;
        this.single = row == null || row.rtmZtbz != 1 || row.rtmTjflag != 0;
        this.listData = row.rtmDjbh;
        this.getListRtdDet(this.listData);
      }
    },
    getListRtdDet(rtmDjbh) {
      listRtdDet(rtmDjbh).then((res) => {
        this.rtdDetList = res.data;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产退料入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rtmId = row.rtmId || this.ids;
      getProMat(rtmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产退料入库";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产退料入库";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.rtmId != null) {
            updateProMat(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.single = true;
              this.getList();
            });
          } else {
            this.form.rtmJlry = this.$store.state.user.nickName;
            this.form.rtmBmdm = this.$store.state.user.deptName;
            addProMat(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.single = true;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rtmIds = row.rtmId || this.ids;
      this.$confirm(
        '是否确认删除生产退料入库编号为"' + rtmIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delProMat(rtmIds);
        })
        .then(() => {
          this.queryParams.pageNum = 1;
          this.proMatStartList = [];
          this.total = 0;
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 添加物料 */
    handleAddChild(row) {
      this.addChildOpen = true;
      this.queryChildParams.rtdCkdm = row.rtmCkdm;
      this.queryChildParams.rtdDjbh = row.rtmDjbh;
      this.rtmDjbhs = row.rtmDjbh;
      this.getSerachChild();
    },
    getListData() {},
    getSerachChild() {
      this.queryChildParams.pageNum = 1;
      serachChild(this.queryChildParams).then((res) => {
        this.searchChildList = [];
        this.searchChildList = this.searchChildList.concat(res.rows);
        this.total2 = res.total;
      });
    },
    load2() {
      if (
        this.queryChildParams.pageNum <
        Math.ceil(this.total2 / this.queryChildParams.pageSize)
      ) {
        this.queryChildParams.pageSize = 40;
        this.queryChildParams.pageNum++;
        serachChild(this.queryChildParams).then((res) => {
          this.searchChildList = this.searchChildList.concat(res.rows);
          this.total2 = res.total;
        });
      }
      return 0;
    },
    /** 确认添加提交物料*/
    searchAddRtd() {
      const datas = this.searchChildList
        .filter((a) => {
          return !!a.rtdSqsl;
        })
        .map((i) => {
          i.rtdDjbh = this.rtmDjbhs;
          i.rtdCwdm = i.rtdCwdm;
          return i;
        });
      if (datas.length < 1) {
        this.msgError("申请数量不能为0");
        return;
      }
      insRtdDet(datas)
        .then((res) => {
          this.msgSuccess("添加成功~");
          this.queryChildParams.pageNum = 1;
          this.getSerachChild();
          //回显字表
          this.getListRtdDet(this.listData);
        })
        .catch(() => {
          this.msgError("添加失败~");
        });
    },
    /** 删除按钮操作 */
    deleteChild(row) {
      removeRtdDet(row.rtdId)
        .then((res) => {
          this.msgSuccess("删除成功~");
          this.getListRtdDet(this.listData);
        })
        .catch(() => {
          this.msgError("删除失败~");
        });
    },
    /** 提交操作按钮 */
    handleSubMit() {
      const data = { rtmId: this.ids, rtmZtbz: 1, rtmTjflag: 1 };
      return updateProMat(data).then((res) => {
        this.msgSuccess("提交成功~");
        this.check_submit = false;
        this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport(index) {
      if (index == 2 && this.rtmDjbhs == null) {
        this.msgError("导出子项前,请先选择单据");
        return;
      }
      let queryParams = this.queryParams;
      queryParams.index = index;
      queryParams.rtmDjbh = this.rtmDjbhs;
      exportProMat(queryParams).then((response) => {
        this.download(response.msg);
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
